package org.jeecg.modules.recruit.appuser.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jodd.util.StringUtil;
import org.apache.commons.collections.CollectionUtils;
import org.jeecg.modules.recruit.appuser.entity.MmAppUser;
import org.jeecg.modules.recruit.appuser.entity.MmAppUserAuth;
import org.jeecg.modules.recruit.appuser.mapper.MmAppUserAuthMapper;
import org.jeecg.modules.recruit.appuser.mapper.MmAppUserMapper;
import org.jeecg.modules.recruit.appuser.service.IMmAppUserService;
import org.jeecg.modules.recruit.popularize.entity.MmAppUserPopularize;
import org.jeecg.modules.recruit.popularize.service.IMmAppUserPopularizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: app用户信息
 * @Author: jeecg-boot
 * @Date: 2020-08-23
 * @Version: V1.0
 */
@Service
public class MmAppUserServiceImpl extends ServiceImpl<MmAppUserMapper, MmAppUser> implements IMmAppUserService {

    @Autowired
    private MmAppUserMapper mmAppUserMapper;
    @Autowired
    private MmAppUserAuthMapper mmAppUserAuthMapper;

    @Autowired
    private IMmAppUserPopularizeService mmAppUserPopularizeService;

    @Override
    @Transactional
    public void saveMain(MmAppUser mmAppUser, List<MmAppUserAuth> mmAppUserAuthList) {
        mmAppUserMapper.insert(mmAppUser);
        if (mmAppUserAuthList != null && mmAppUserAuthList.size() > 0) {
            for (MmAppUserAuth entity : mmAppUserAuthList) {
                //外键设置
                entity.setUserId(mmAppUser.getId());
                mmAppUserAuthMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional
    public void updateMain(MmAppUser mmAppUser, List<MmAppUserAuth> mmAppUserAuthList) {
        mmAppUserMapper.updateById(mmAppUser);

        //1.先删除子表数据
        mmAppUserAuthMapper.deleteByMainId(mmAppUser.getId());

        //2.子表数据重新插入
        if (mmAppUserAuthList != null && mmAppUserAuthList.size() > 0) {
            for (MmAppUserAuth entity : mmAppUserAuthList) {
                //外键设置
                entity.setUserId(mmAppUser.getId());
                mmAppUserAuthMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional
    public void delMain(String id) {
        mmAppUserAuthMapper.deleteByMainId(id);
        mmAppUserMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            mmAppUserAuthMapper.deleteByMainId(id.toString());
            mmAppUserMapper.deleteById(id);
        }
    }

    @Override
    public boolean minusAmount(String userId, Integer amount) {
        int i = mmAppUserMapper.minusAmount(userId, amount);
        return i > 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public MmAppUser register(MmAppUser mmAppUser, String popularizeId) {
        List<MmAppUser> historyList = lambdaQuery().eq(MmAppUser::getOpenid, mmAppUser.getOpenid()).list();
        if (CollectionUtils.isNotEmpty(historyList)) {
            //todo 更新用户信息
            mmAppUser = historyList.get(0);
        } else {
            save(mmAppUser);
            if (StringUtil.isNotEmpty(popularizeId)) {
                List<MmAppUser> list = lambdaQuery().eq(MmAppUser::getPhoneNo, popularizeId).list();
                if (CollectionUtils.isEmpty(list)) {
                    //添加推广记录
                    MmAppUserPopularize popularize = new MmAppUserPopularize();
                    popularize.setUserId(mmAppUser.getId());
                    popularize.setPUserId(list.get(0).getId());
                    mmAppUserPopularizeService.save(popularize);
                }
            }
        }
        return mmAppUser;
    }


}
